package com.atguigu.Flink.state;

import com.atguigu.Flink.POJO.WindSpeedSensor;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class Flink08_KeyedReducingState {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //需求：检测每种传感器的风速，如果连续的两个风速差值为10，就输出报警
        SingleOutputStreamOperator<WindSpeedSensor> ds = env.socketTextStream("hadoop102", 8888)
                .map(
                        line -> {
                            String[] split = line.split(",");
                            return new WindSpeedSensor(split[0], Integer.valueOf(split[1]), Long.valueOf(split[2]));
                        }
                );
            //去掉每种传感器的风速和
                ds.keyBy(
                        k->k.getId()
                ).process(
                        new KeyedProcessFunction<String, WindSpeedSensor, String>() {
                            //声明归约状态
                            private ReducingState<Integer> reducingState;

                            @Override
                            public void open(Configuration parameters) throws Exception {
                                //初始化状态
                                ReducingStateDescriptor<Integer> reducingStateDescriptor = new ReducingStateDescriptor<Integer>(
                                        "reduceState"
                                        , new ReduceFunction<Integer>() {
                                    @Override
                                    public Integer reduce(Integer integer, Integer t1) throws Exception {
                                        return integer + t1;
                                    }
                                },
                                        Types.INT
                                );
                                reducingState = getRuntimeContext().getReducingState(reducingStateDescriptor);
                            }

                            @Override
                            public void processElement(WindSpeedSensor windSpeedSensor, KeyedProcessFunction<String, WindSpeedSensor, String>.Context context, Collector<String> collector) throws Exception {
                                //将当前传感器的风速加入到状态
                                reducingState.add(windSpeedSensor.getSpeed());
                                collector.collect(windSpeedSensor.getId() + "当前风速和" + reducingState.get());
                            }
                        }
                ).print();



        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
